

#### generate basis function
basis=function(treat, X){
  D=treat%*%solve(chol(var(treat)))
  D=scale(D,center=TRUE, scale=TRUE)
  X=X%*%solve(chol(var(X)))
  X=scale(X,center=TRUE, scale=TRUE)
  X=X%*%diag(as.vector(sign(cor(X,D))),nrow=ncol(X))
  D=D%*%diag(as.vector(sign(cor(X,D))),nrow=ncol(D))
  z<-matrix(NA,ncol=ncol(D)*ncol(X),nrow=nrow(D))
  for(i in 1:ncol(D)){
    z[,((i-1)*ncol(X)+1):(i*ncol(X))]=X*as.vector(D[,i])
  }
  z=cbind(z,X,D)
  return(z)
}

